# -*- coding: utf-8 -*-
# @Time : 2022/6/8 下午1:43
# @Author : wyf
# @FileName: 作业5.py
# @Email: yunfei.wang@innvote.com
import requests
from pymongo import MongoClient


class GetNews(object):

    def __init__(self):
        self.client = MongoClient("121.41.206.74")
        self.db = self.client["python"]['day5']

    def __del__(self):
        self.client.close()

    @staticmethod
    def get_data(page=1, size=20):
        _url = f"http://da.wa.news.cn/nodeart/page?nid=11203988&pgnum={page}&cnt={size}&attr=&tp=1&orderby=1"
        res = requests.get(url=_url)
        try:
            data = res.json().get("data").get("list")
        except Exception as e:
            print(f"爬取任务出错, {e}")
            data = []
        return data

    def save_data(self, item: dict):
        self.db.insert_one(item)

    def run(self, page=1):
        for new in self.get_data(page=page):
            _dict = {
                "title": new.get("Title"),
                "editor": new.get("Editor"),
                "time": new.get("PubTime"),
                "detail_url": new.get("LinkUrl"),
                "brief": new.get("Abstract"),
            }
            self.save_data(_dict)


if __name__ == '__main__':
    GetNews().run(3)



